package com.gfdd.tea.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.gfdd.tea.service.WeChatService;
import com.gfdd.tea.utlis.WeChatUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class WeChatServiceImpl implements WeChatService {
//    @Override
//    public String codetoopenid(String code) {
//        Map<String, String> data = new HashMap<String, String>();
//        data.put("appid", "wx434281b5d3211420");
//        data.put("secret", "67edf6ac8f748a375ca209447d103e15");
//        data.put("js_code", code);
//        data.put("grant_type", "authorization_code");
//
//
//        String response = HttpRequest.get("https://api.weixin.qq.com/sns/jscode2session").form(data).body();
//        System.out.println("Response was: " + response);
//        JSONObject obj= JSON.parseObject(response);//将json字符串转换为json对
//        System.out.println(obj);
//
//        return null;
//    }

    @Override
    public JSONObject authCode2Session(String appId, String secret, String jscode) {
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appId + "&secret=" + secret + "&js_code" + jscode + "&grant_type=authorization";
        String str = WeChatUtil.httpRequest(url, "GET", null);
        log.info("api/wx-mini/getSessionKey:" + str);
        if (StringUtils.isEmpty(str)) {
            return null;
        } else {
            return JSONObject.parseObject(str);
        }
    }
}
